﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration; // 用来读取配置文件

namespace 人员管理
{



    public partial class Form2 : Form
    {
        PersonService personService;

        public delegate void RefreshDelegate(); // 子窗口声明定义委托 refresh()
        public event RefreshDelegate refresh;

        public DataSet DS
        {
            get;
            set;
        }

        public Person person
        {
            get;set;
        }

        public Form2()
        {
            InitializeComponent();

            personService = new PersonService();
        }

        public void initData(Person p)
        {
            textBox1.Text = p.Name;
            textBox2.Text = Convert.ToString(p.Age);
            comboBox1.Text = p.Sex;
            textBox4.Text = p.Addr;
        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DoSave(object sender, EventArgs e)
        {

            int id = person.ID;
            if(person != null && id != -999)
            {
                person.Name = textBox1.Text;
                person.Age = Convert.ToInt32(textBox2.Text);
                person.Sex = comboBox1.Text;
                person.Addr = textBox4.Text;

                int rows = personService.updatePerson(person);
                if (rows > 0)
                {
                    this.Close();
                    MessageBox.Show("保存成功");
                    this.refresh(); // 调用委托
                }
                else
                {
                    this.Close();
                    MessageBox.Show("保存失败");
                }
            }
            else
            {
                person.Name = textBox1.Text;
                person.Age = Convert.ToInt32(textBox2.Text);
                person.Sex = comboBox1.Text;
                person.Addr = textBox4.Text;

                int rows = personService.addPerson(person);
                if (rows > 0)
                {
                    this.Close();
                    MessageBox.Show("保存成功");
                    this.refresh(); // 调用委托
                }
                else
                {
                    this.Close();
                    MessageBox.Show("保存失败");
                }
            }
        }

        private void DoCancel(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
